package in.muton.edushare.poll.questions;

import in.muton.edushare.pages.EduShareBasePage;
import in.muton.edushare.wicket.models.CayenneList;

import java.util.Collections;
import java.util.List;

import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.form.Radio;
import org.apache.wicket.markup.html.form.RadioGroup;
import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.markup.html.list.ListView;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.Model;
import org.apache.wicket.model.PropertyModel;

import com.edushare.common.dao.UserPollDAO;
import com.edushare.common.vo.PollOptions;
import com.edushare.common.vo.PollQuestions;

public class RadioQuestion extends QuestionBase {

	public RadioQuestion(String id, IModel<PollQuestions> model,
			EduShareBasePage page, final boolean preview, final boolean editMode) {
		super(id, model, page, preview, editMode);
		RadioGroup<Integer> rg = null;
		add(rg = new RadioGroup<Integer>("radioGroup",
				new PropertyModel<Integer>(this, "selection")));

		List<PollOptions> poList = getQuestionModel().getObject().getOptions();
		Collections.sort(poList);
		CayenneList<PollOptions> po = new CayenneList<PollOptions>(poList);
		retrieveValues();
		rg.add(new ListView<IModel<PollOptions>>("optionsList", po) {
			private static final long serialVersionUID = 286372665924118868L;

			@Override
			protected void populateItem(ListItem<IModel<PollOptions>> arg0) {
				PollOptions p = arg0.getModelObject().getObject();
				arg0.add(new Radio<Integer>("option", new Model<Integer>(p
						.getOptionId())));
				arg0.add(new Label("optionName", p.getOptionString()));
			}
		});
	}

	private Integer selection = null;
	private static final long serialVersionUID = 7079929247501262875L;

	public void setSelection(Integer selection) {
		this.selection = selection;
	}

	public Integer getSelection() {
		return selection;
	}

	@Override
	public boolean retrieveValues() {
		UserPollDAO upDAO = new UserPollDAO(getORMInterface());
		List<PollOptions> poList = upDAO.getAnswer(getLoggedInUser(),
				getQuestionModel().getObject());
		if (poList.size() != 1)
			return false;
		setSelection(poList.get(0).getOptionId());
		return true;
	}

	@Override
	public boolean save(boolean draft) {
		if (isPreview() || isEditMode() || (!draft && !verify()))
			return false;
		UserPollDAO uPDao = new UserPollDAO(getORMInterface());
		uPDao.setAnswer(getLoggedInUser(), getQuestionModel().getObject(),
				getSelection());
		return true;
	}

	@Override
	public boolean verify() {
		if (getSelection() == null
				&& getQuestionModel().getObject().isRequired()) {
			setError("Please make a selection in question no. "
					+ getQuestionModel().getObject().getQuestionSerialNo());
			return false;
		}
		return true;
	}

	@Override
	public void clear() {
		new UserPollDAO(getORMInterface()).clearAnswers(getLoggedInUser(),
				getQuestionModel().getObject());
		setSelection(null);
	}
}
